<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Results
 *
 * @author fun
 */
class LeagueResults  extends Results
{
  
  public $players = array();

  public $resultOrder = array();
  
  public $resultTable = array();
  
  public function  __construct($players, $league) {
    
    $this->players = $players;
   
    foreach($this->players as $player)
    {
      $this->resultTable[$player->getId()] = array();
      
      $this->resultTable[$player->getId()]['object'] = $player;
    }
    
    parent::__construct($league);
    
  }
  
  public function  getLeagueResults() {
    
    $con = Propel::getConnection();

    //Prende la conta dei match giocati e la somma dei punteggi per ogni player
    $sql = "SELECT " . PlayerGamePeer::ID_PLAYER . ",
      COUNT(" . PlayerGamePeer::ID . ") AS n_match,
      SUM(" . PlayerGamePeer::PTS . ") AS pts_tot
      FROM " . PlayerGamePeer::TABLE_NAME . "
      INNER JOIN `" . GamePeer::TABLE_NAME . "` ON " . GamePeer::ID . " = " . PlayerGamePeer::ID_GAME . "
      WHERE " . GamePeer::ID_LEAGUE . " = ?
      GROUP BY " . PlayerGamePeer::ID_PLAYER;
    
    //die($sql);

    $stmt = $con->prepare($sql);
    $stmt->bindValue(1, $this->league->getId());

    $stmt->execute();
    
    while($row = $stmt->fetch()) {
      //var_dump($row);
      $this->resultTable[$row['ID_PLAYER']]['results'] = $row;
    }

    //Prende il numero di vittorie raggruppate per numerosità dei giocatori per ogni player
    $sql2 = "SELECT " . PlayerGamePeer::ID_PLAYER . ",
      m_tot.n_player, 
      COUNT(" . PlayerGamePeer::ID . ") AS n_win
      FROM " . PlayerGamePeer::TABLE_NAME . "
      INNER JOIN (SELECT " . PlayerGamePeer::ID_GAME . ", COUNT(" . PlayerGamePeer::ID . ") AS n_player FROM " . PlayerGamePeer::TABLE_NAME . " GROUP BY " . PlayerGamePeer::ID_GAME . ") m_tot ON m_tot.ID_GAME = " . PlayerGamePeer::ID_GAME . "
      WHERE " . PlayerGamePeer::PLACING . " = ?
      GROUP BY " . PlayerGamePeer::ID_PLAYER . ", m_tot.n_player
      ORDER BY " . PlayerGamePeer::ID_PLAYER . ", m_tot.n_player";

    $stmt2 = $con->prepare($sql2);
    $stmt2->bindValue(1, 1);

    $stmt2->execute();
    
    while($row2 = $stmt2->fetch()) {
      //var_dump($row);
      
      if(!isset($this->resultTable[$row2['ID_PLAYER']]['winner_det']))
      {
        $this->resultTable[$row2['ID_PLAYER']]['winner_det'] = array();
      }
      
      if(!isset($this->resultTable[$row2['ID_PLAYER']]['winner_tot']))
      {
        $this->resultTable[$row2['ID_PLAYER']]['winner_tot'] = 0;
      }
      
      $this->resultTable[$row2['ID_PLAYER']]['winner_det'][] = $row2;
      
      $this->resultTable[$row2['ID_PLAYER']]['winner_tot'] += $row2['n_win'];
    }
    
    //die(var_dump($this->resultTable));
    
    $this->setSort();

    return $this->resultTable;
                
  }
  
  private function setSort()
  {
    $temp = array();
    
    foreach($this->players as $player)
    {
      $temp[$player->getId()] = (isset($this->resultTable[$player->getId()]['results']['pts_tot'])) ? $this->resultTable[$player->getId()]['results']['pts_tot'] : 0;
    }
    
    arsort($temp);
    
    $this->resultOrder = array_keys($temp);
    
    $this->sortResultTable();
  }
  
  private function sortResultTable()
  {
    $temp = array();
    
    foreach($this->resultOrder as $idPlayer)
    {
      $temp[$idPlayer] = $this->resultTable[$idPlayer];
    }
    
    $this->resultTable = $temp;
  }
}
?>